/*
 * @Description:
 * @Author: liyuhao
 * @Date: 2021-07-12 15:22:56
 * @LastEditors: liyuhao
 * @LastEditTime: 2021-12-15 13:40:39
 */
import { getType } from '@/utils/util'

export interface Pagination {
  current?: number
  pageSize?: number
}

// const orderKey: {
//   [key: string]: string
// } = {
//   ascend: 'asc',
//   descend: 'desc'
// }

// 所有使用useRequest的分页接口都需要调用此函数处理入参数
export const formatPaginatedParams = (
  params?: any,
  pageKey = 'current',
  sizeKey = 'size',
  startPage = 1
) => {
  // 暂不支持多列同时排序
  const { current, pageSize, filters, sorter, ...otherParams } = params || {}

  const page =
    typeof current === 'number' && !isNaN(current) && current > 0
      ? current - (1 - startPage)
      : 0
  // const { field, order } = sorter || {}

  return {
    ...otherParams,
    [pageKey]: page,
    [sizeKey]: pageSize
    // sortName: field,
    // sortType: orderKey[order as string]
  }
}

export const formatPaginatedResult = (
  res?: any,
  listKey = 'records',
  totalKey = 'total'
) => {
  const { [listKey]: _list, [totalKey]: _total, ...otherRes } = res || {}
  const list = getType(_list) === 'Array' ? _list : []
  const total = typeof _total === 'number' && !isNaN(_total) ? _total : 0

  return {
    ...otherRes,
    list,
    total
  }
}
